﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Spring.Data;
using Spring.Data.Generic;
using Spring.Data.Common;
using Spring.Data.Objects.Generic;
using Spring.Transaction;
using Spring.Transaction.Support;
using System.Collections;


namespace Ordering
{
    //public enum GroupType { MyOffice, MyDepartment, MyTeam, MyFriend }
    //public enum IsFriend { No, Yes }

    class  OrderBoardDao
    {
        #region Definition of Fields
        private static AdoTemplate template = AppConfigurer.AdoTemplate;
        static SqlConnection conn = new DBConn().Connect();
        #endregion

        internal static IList<IDictionary<string, string>> ListOrders(GroupType type, string staffId)
        {
            // The sql for query
            string baseSql = "select ao.Staff_Id as StaffId, st.Staff_Name as StaffName, " +
                "st.Staff_Dept as StaffDept, di.Dish_Name as DishName, ao.Order_Datetime as OrderDatetime from Orders ao " +
                "left join Staff st on ao.Staff_Id = st.Staff_Id " +
                "left join Dish di on ao.Dish_Id = di.Dish_Id " +
                "left join Shop sh on di.Shop_Id = sh.Shop_Id ";

            string whereSql = null;
            switch (type)
            {
                case GroupType.MyDepartment:
                    break;
                case GroupType.MyTeam:
                    whereSql = "where ao.Staff_Id in ( " +
                        "select Staff_Id from Teammate " +
                        "where Team_Id in ( " +
                        "select Team_Id from Teammate " +
                        "where Staff_Id = @staffId ))";
                    break;
                case GroupType.MyFriend:
                    whereSql = "where ao.Staff_Id in ( " +
                        "select Friend_Id from Friend " +
                        "where Staff_Id = @staffId )";
                    break;
                default:
                    whereSql = null;
                    break;
            }

            string orderSql = "order by ao.Staff_Id, ao.Order_DateTime ";
            string strSql = baseSql + whereSql + orderSql;

            IDbParameters para = template.CreateDbParameters();
            para.Add("staffId", DbType.String).Value = staffId;

            IList<IDictionary<string, string>> lst = new List<IDictionary<string, string>>();            
            template.QueryWithRowCallbackDelegate(CommandType.Text, strSql, delegate(IDataReader dr)
            {
                IDictionary<string, string> dict = new Dictionary<string, string>();
                // dict.Add("DishName", dr.GetString(3));
                dict.Add("StaffId", dr.GetString(0));
                dict.Add("StaffName", dr.GetString(1));
                dict.Add("StaffDept", dr.GetString(2));   
                dict.Add("DishName", dr.GetString(3));
                dict.Add("OrderDatetime", dr.GetDateTime(4).ToString());   
                lst.Add(dict);
            }, para);

            return lst;
        }

        internal static IList<IDictionary<string, string>> ListTeams()
        {
            string strSql = "select * from [Team]";
            IDbParameters para = template.CreateDbParameters();

            IList<IDictionary<string, string>> lst = new List<IDictionary<string, string>>();
            template.QueryWithRowCallbackDelegate(CommandType.Text, strSql, delegate(IDataReader dr)
            {
                IDictionary<string, string> dicValue = new Dictionary<string, string>();
                dicValue.Add("Team_Name", dr.GetString(1));
                dicValue.Add("Team_Id", dr.GetString(0));

                lst.Add(dicValue);
            }, para);
            return lst;
        }

        internal static IList<Team> ListTeams2()
        {
            string strSql = "select * from [Team]";
            IDbParameters para = template.CreateDbParameters();
            IList<Team> lst = new List<Team>();
            template.QueryWithRowCallbackDelegate(CommandType.Text, strSql, delegate(IDataReader dr)
            {
                Team team = new Team();
                team.TeamId = dr.GetString(0);
                team.TeamName = dr.GetString(1);
                team.TeamCreateTime = dr.GetDateTime(2).ToString();
                lst.Add(team);
            }, para);
            return lst;
        }

        #region team

        internal int createTeam(string userId)
        {
            throw new NotImplementedException();
        }

        internal int joinTeam(string userId, string teamId)
        {
            throw new NotImplementedException();
        }

        internal int inviteToTeam(string userId, string frindId)
        {
            throw new NotImplementedException();
        }

        internal int quitTeam(string userId, string teamId)
        {
            throw new NotImplementedException();
        }

        #endregion 

        #region FriendDao

        internal static int AddFriend(string staffId, string friendId)
        {
            string strSql = "insert into Friend values(@staffId, @friendId)";
            IDbParameters para = template.CreateDbParameters();
            para.Add("staffId", DbType.String).Value = staffId;
            para.Add("friendId", DbType.String).Value = friendId;
            return template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        internal static int removeFriend(string staffId, string friendId)
        {
            string strSql = "delete from Friend where Staff_Id = @staffId and Friend_Id = @friendId";
            IDbParameters para = template.CreateDbParameters();
            para.Add("staffId", DbType.String).Value = staffId;
            para.Add("friendId", DbType.String).Value = friendId;
            return template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        internal static bool isFriend(string staffId, string friendId)
        {
            string strSql = "select count(*) from Friend where Staff_Id = @staffId and Friend_Id = @friendId";
            IDbParameters para = template.CreateDbParameters();
            para.Add("staffId", DbType.String).Value = staffId;
            para.Add("friendId", DbType.String).Value = friendId;
            int cnt = (int)template.ExecuteScalar(CommandType.Text, strSql, para);
            return cnt == 0 ? false : true;
        }

        #endregion
    }
}
